import {
    access
} from "@/api/core/user";
import store from '@/utils/store/cache'
import { queryTree } from "@/utils/index";
import { construct, destruct } from "@aximario/json-tree";
import { set } from "@/store/utils/mutations";
import { dbSet, dbGet } from "@/store/utils/action";
export default {
    namespaced: true,
    state: {
        // 权限数据
        access: [],
        // 菜单数据
        aside: [],
        left: []
    },
    actions: {
        dbSet,
        dbGet,
        /**
         * 请求用户权限数据
         * @param {Object} context
         */
        async accessLoad(context: any, isReLoad = false) {
            store.loadCacheDataByFun(context, 'aside', access, {}, {
                isReLoad,
                mutationName: 'aside',
                readerTransform: function (data) {
                    if (data.length && data[0].menu_name) {
                        // 接口数据进行转化
                        return destruct(data, {
                            id: "menu_sn",
                            pid: 'menu_psn'
                        })
                    }
                    console.log(data)
                    return data;
                },
                disposeItem: function (item: any) {
                    if (item.menu_name) {
                        // 接口数据进行转化
                        item.label = item.menu_name
                        item.id = item.menu_sn
                        item.path = item.menu_path
                        item.type = item.menu_type
                        item.parentid = item.menu_psn
                    }
                    const path = item.path;
                    if (!path) {
                        item.path = item.id.toString();
                    }
                    // 在这里处理数据，避免改动主容器代码
                    item.webPerms = path;
                }
            });
        }
    },
    getters: {
        // 权限数据
        access(state: any) {
            return state.access;
        },
        // 菜单数据
        aside(state: any) {
            // console.log(state.aside);
            return state.aside;
        },
        // 菜单数据
        left(state: any) {
            // console.log(state.aside);
            return state.left;
        }
    },
    mutations: {
        set,
        aside(state: any, params: any) {
            const treeData = construct(params.data, {
                pid: 'parentid'
            });
            // console.log(treeData);
            state.access = treeData;
            // 数据新增层级
            // eachTree(treeData);
            // 过滤掉权限数据
            state.aside = queryTree(treeData, function (item: any) {
                return [0, 1, 2].includes(item.type);
            }, {
                // 按orderNum排序
                orderBy: 'orderNum'
            });
        }
    }
}